﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace EditPlatformTool
{
    public class FileProcess
    {
        private static FileProcess _pInstance = null;
        private FileStream _fstream = null;
        private StreamReader _rstream = null;
        private StreamWriter _wstream = null;
        private Encoding _DetectEncode = null;

        private FileProcess() { }
        public static FileProcess GetInstance()
        {
            if (_pInstance == null)
                _pInstance = new FileProcess();
            return _pInstance;
        }

        public void Create(String path)
        {
            if (File.Exists(path))
            {
                FileStream f = new FileStream(path, FileMode.Open);
                StreamReader r = new StreamReader(f);
                _DetectEncode = r.CurrentEncoding;
                r.Close();

                LogControl.PrintLog(LogControl.LogType.Info, "file " + path + " is exist.");
                File.Delete(path);
            }

            try
            {
                LogControl.PrintLog(LogControl.LogType.Info, "Create file " + path);
                _fstream = new FileStream(path, FileMode.Create);
            }
            catch (Exception e)
            {
                LogControl.PrintLog(LogControl.LogType.Error, e.Message);
            }

            if (_fstream == null)
                LogControl.PrintLog(LogControl.LogType.Error, "Can't create file " + path);
        }
        public void Open(String path)
        {
            if(!File.Exists(path))
            {
                LogControl.PrintLog(LogControl.LogType.Error, "file " + path + " isn't exist.");
                return;
            }

            try
            {
                LogControl.PrintLog(LogControl.LogType.Info, "Open file " + path);
                _fstream = new FileStream(path, FileMode.Open);
            }catch(Exception e)
            {
                LogControl.PrintLog(LogControl.LogType.Error, e.Message);
            }

            if(_fstream == null)
                LogControl.PrintLog(LogControl.LogType.Error, "Can't open file "+path);
        }
        public void Close()
        {
            if(_fstream != null)
            {
                LogControl.PrintLog(LogControl.LogType.Info, "Close file!");
                if (_rstream != null) _rstream.Close();
                if (_wstream != null) _wstream.Close();
                _fstream.Close();
            }
        }

        public string Readto(ref string readData, char key)
        {
            string Keyword = "";

            _rstream = new StreamReader(_fstream);
            while(_rstream.EndOfStream)
            {
                char c = (char)_rstream.Read();
                if(c==key)
                {
                    Keyword += _rstream.Read();
                    Keyword += _rstream.Read();
                    Keyword += _rstream.Read();
                    Keyword += _rstream.Read();
                    return Keyword;
                }
                readData += c;
            }

            return Keyword;
        }
        public String ReadLine()
        {
            String result = "";
            if(_fstream == null)
            {
                LogControl.PrintLog(LogControl.LogType.Error, "There are no file stream.");
                return result;
            }

            _rstream = new StreamReader(_fstream);
            if(_rstream == null)
            {
                LogControl.PrintLog(LogControl.LogType.Error, "Can't create stream reader.");
                return result;
            }

            result = _rstream.ReadLine();

            return result;
        }
        public void Write(String data)
        {
            if (_fstream == null)
            {
                LogControl.PrintLog(LogControl.LogType.Error, "There are no file stream.");
                return;
            }
            LogControl.PrintLog(LogControl.LogType.Info, _DetectEncode.EncodingName);
            _wstream = new StreamWriter(_fstream, _DetectEncode);
            if (_wstream == null)
            {
                LogControl.PrintLog(LogControl.LogType.Error, "Can't create stream writter.");
                return;
            }

            LogControl.PrintLog(LogControl.LogType.Info, "Write data");
            _wstream.Write(data);
        }
    }
}
